package android.tkb.activity.word;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.tkb.dao.word.DatabaseHelper;
import android.tkb.util.word.Contants;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 主类（主界面）
 * @author Administrator
 *
 */
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
	
	private ArrayList<Map<String,Object>> data;
	private ListView lv;

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        lv= (ListView) findViewById(R.id.lv);
        data = loadData();
        SimpleAdapter adapter  = new SimpleAdapter(this,data,R.layout.main_list,new String[]{"img","name"},new int[]{R.id.img,R.id.name});
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new ListViewOnItemClickListener());
        
        createDB();
      
		//SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    }
    
    public void createDB(){
    	  DatabaseHelper mOpenHelper = new DatabaseHelper(MainActivity.this);//创建数据库
          try {
  			mOpenHelper.createDataBase();
  		} catch (IOException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
    }
    /**
     * ListView事件临听器
     * @author Administrator
     *
     */
    class ListViewOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			createDB();
			
			Intent intent = new Intent();
			System.out.println(position);
			if(position==0){//英——>中
				intent.setClass(MainActivity.this, EnOrChTestActivity.class);
				intent.putExtra("key", Contants.E);
				startActivity(intent);
			}else if(position==1){
				intent.setClass(MainActivity.this, EnOrChTestActivity.class);
				intent.putExtra("key", Contants.C);
				startActivity(intent);
			}else if(position==2){
				intent.setClass(MainActivity.this, SearchWordActivity.class);
				startActivity(intent);
			}else if(position==3){//进入易错单词页面
				intent.setClass(MainActivity.this, EasyWordActivity.class);
				startActivity(intent);
			}
			
		}
    	
    }
    
    public ArrayList<Map<String,Object>> loadData(){
    	ArrayList<Map<String,Object>> coll = new ArrayList<Map<String,Object>>();
    	Map<String,Object> item =  new HashMap<String,Object> ();
    
    	item.put("img", R.drawable.wordtexten);
		item.put("name", "单词测试(英——>中)");
		coll.add(item);
		
		item =  new HashMap<String,Object> ();
		item.put("img", R.drawable.wordtextch);
		item.put("name", "单词测试(中——>英)");
		coll.add(item);
		
		item =  new HashMap<String,Object> ();
		item.put("img", R.drawable.search);
		item.put("name", "单词搜索");
		coll.add(item);
		
		item =  new HashMap<String,Object> ();
		item.put("img", R.drawable.easyerror);
		item.put("name", "易错单词");
		coll.add(item);
    	
    	return coll;
    
    }
    
    
}